package cn.tedu.coolshark.controller;

import cn.tedu.coolshark.mapper.ProductMapper;
import cn.tedu.coolshark.pojo.entity.Product;
import cn.tedu.coolshark.pojo.vo.ProductIndexVO;
import cn.tedu.coolshark.pojo.vo.ProductTopVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.Date;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/product")
@Api(tags = "商品模块管理")
public class ProductController {
    @Autowired
    private ProductMapper productMapper;

    @GetMapping("/select")
    @ApiOperation(value = "查询所有商品")
    public List<Product> created() {
        return productMapper.selectAll();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "根据id删除商品")
    @ApiImplicitParam(name = "id",value = "商品id",required = true)
    public void delete(Integer id) {
        Product product = productMapper.selectById(id);
        File file = new File("coolshark/src/main/resources/static"+product.getUrl());
        file.delete();
        productMapper.delete(id);
    }

    @PostMapping("/insert")
    @ApiOperation(value = "添加商品")
    @ApiImplicitParam(name = "product",value = "商品实体类",required = true)
    public int insert(@RequestBody Product product){
        Product newProduct = new Product();
        BeanUtils.copyProperties(product,newProduct);
        newProduct.setCreated(new Date());
        newProduct.setViewCount(0);
        String oldUrl = product.getUrl();
        int num ;
        if(productMapper.selectMaxId() == null){
            num=1;
        }else{
            num = productMapper.selectMaxId()+1;
        }
        String newUrl = "/imgs/p"+num+"."+oldUrl.split("\\.")[1];
        newProduct.setUrl(newUrl);
        ControllerUtils.rename(oldUrl,newUrl);
        return productMapper.insert(newProduct);
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新商品")
    @ApiImplicitParam(name = "product",value = "商品实体类",required = true)
    public int update(@RequestBody Product product){
        Product oldProduct = productMapper.selectById(product.getId());
        ControllerUtils.rename(product.getUrl(),oldProduct.getUrl());
        product.setUrl(oldProduct.getUrl());
        return productMapper.update(product);
    }

    @GetMapping("/selectById")
    @ApiOperation(value = "根据id查询商品")
    @ApiImplicitParam(name = "id",value = "商品id",required = true)
    public Product selectById(Integer id){
        return productMapper.selectById(id);
    }

    @GetMapping("/top")
    @ApiOperation(value = "查询首页商品")
    public List<ProductTopVO> top(){
        return productMapper.selectIndex();
    }

    @GetMapping("/category")
    @ApiOperation(value = "根据分类id查询商品")
    @ApiImplicitParam(name = "id",value = "分类id",required = true)
    public List<ProductIndexVO> category(Integer id){
        log.debug("categoryId="+id);
        return productMapper.selectByCId(id);
    }

    @GetMapping("/index")
    @ApiOperation(value = "查询首页商品")
    public List<ProductIndexVO> index(){
        return productMapper.selectIndexVO();
    }

    @GetMapping("/search")
    @ApiOperation(value = "根据关键字查询商品")
    @ApiImplicitParam(name = "wd",value = "关键字",required = true)
    public List<ProductIndexVO> search(String wd){
        return productMapper.selectByWd(wd);
    }

    @GetMapping("/detail")
    @ApiOperation(value = "根据id查询商品详情")
    @ApiImplicitParam(name = "id",value = "商品id",required = true)
    public Product detail(Integer id){
        productMapper.updateViewById(id);
        return productMapper.selectById(id);
    }
}
